package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelProxyFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHandleHttpReportClientProxyServerTransferResponseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProtocolType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProxyType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.proxy.ServerChannelProxyFlow;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;


/**
 * 服务端处理客户端代理到服务端的请求
 * HTTP_REPORT_CLIENT_PROXY_SERVER_TRANSFER_RESPONSE_
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportClientProxyServerTransferResponseTypeAdvanced
        extends AbstractHandleHttpReportClientProxyServerTransferResponseTypeAdvanced<NettyProxyMsg> {

    private final ChannelProxyFlowAdapter channelProxyFlowAdapter;

    public ServerHandleHttpReportClientProxyServerTransferResponseTypeAdvanced(ChannelProxyFlowAdapter channelProxyFlowAdapter) {
        this.channelProxyFlowAdapter = channelProxyFlowAdapter;
    }

    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        Channel channel = nettyChannelContext.channel();

        byte[] data = nettyProxyMsg.getData();

        Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);

        String targetIp = ChannelAttributeKeyUtils.getTargetIp(nextChannel);
        Integer targetPort = ChannelAttributeKeyUtils.getTargetPort(nextChannel);
        // 将数据返回给客户端
        NettyProxyMsg responseNettyProxyMsg = new NettyProxyMsg();
        responseNettyProxyMsg.setType(ProxyMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_SERVER_TRANSFER_);
        responseNettyProxyMsg.setData(data);

        nextChannel.writeAndFlush(responseNettyProxyMsg);

        String clientId = ChannelAttributeKeyUtils.getClientId(nextChannel);
        Integer visitorPort = ChannelAttributeKeyUtils.getVisitorPort(nextChannel);

        // 记录出口数据
        ServerChannelProxyFlow serverChannelFlow = ServerChannelProxyFlow
                .builder()
                .channelFlowEnum(ChannelFlowEnum.OUT_FLOW)
                .port(targetPort)
                .ip(targetIp)
                .clientId(clientId)
                .channelProxyType(ChannelProxyType.CLIENT_PROXY_SEVER)
                .channelProtocolType(ChannelProtocolType.HTTP)
                .flow(data.length)
                .build();
        channelProxyFlowAdapter.asyncHandler(channel, serverChannelFlow);
    }

}
